/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fontys.time;

import java.util.*;

/**
 *
 * @author Administrator
 */
public class Time implements ITime {
    
    private GregorianCalendar backingTime;    
    /**
    * creation of a Time-object with year y, month m, day d, hours h and
    * minutes m; if the combination of y-m-d refers to a non-existing date
    * a correct value of this Time-object will be not guaranteed
    * @param y
    * @param m 1≤m≤12
    * @param d 1≤d≤31
    * @param h 0≤h≤23
    * @param m 0≤m≤59
    */
    public Time(int y, int m, int d, int h, int min){
       if (1 <= m && m <= 12 && 1 <= d && d <= 31
               && 0 <= h && h <= 23 && 0 <= min && min <= 59){
           backingTime = new GregorianCalendar(y, m, d, h, min);
       } 
       else
           throw new IllegalArgumentException("given value was outside the"
                   + "expected range of values, check the documentation for"
                   + "help");
    }
    
    @Override
    public int getYear() {
        return backingTime.get(Calendar.YEAR);
    }

    @Override
    public int getMonth() {
        return backingTime.get(Calendar.MONTH);
    }

    @Override
    public int getDay() {
        return backingTime.get(Calendar.DAY_OF_MONTH);
    }

    @Override
    public int getHours() {
        return backingTime.get(Calendar.HOUR_OF_DAY);
    }

    @Override
    public int getMinutes() {
        return backingTime.get(Calendar.MINUTE);
    }

    @Override
    public DayInWeek getDayInWeek() {
        switch (backingTime.get(Calendar.DAY_OF_WEEK))
        {
            case Calendar.SUNDAY:
                return DayInWeek.SUN;
            case Calendar.MONDAY:
                return DayInWeek.MON;
            case Calendar.TUESDAY:
                return DayInWeek.TUE;
            case Calendar.WEDNESDAY:
                return DayInWeek.WED;
            case Calendar.THURSDAY:
                return DayInWeek.THU;
            case Calendar.FRIDAY:
                return DayInWeek.FRI;
            case Calendar.SATURDAY:
                return DayInWeek.SAT;
            default :
                return DayInWeek.SUN;
        }
    }

    @Override
    public ITime plus(int minutes) {
        GregorianCalendar cal = (GregorianCalendar)backingTime.clone();
        cal.add(Calendar.MINUTE, minutes);
        Time t = new Time(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
                cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR_OF_DAY),
                cal.get(Calendar.MINUTE)); 
        return t;      
    
    }

    @Override
    public int difference(ITime time) {
        GregorianCalendar cal = 
                new GregorianCalendar(time.getYear(), time.getMonth(),
                time.getDay(), time.getHours(), time.getMinutes());
        long x = cal.getTimeInMillis() - this.backingTime.getTimeInMillis();
        x = (x/1000)/60;
        return Math.abs((int)x);
    }

    @Override
    public int compareTo(ITime o) {
        int x = this.getYear()- o.getYear();
        if (x != 0)
            return x;
        else
        {
            x = this.getMonth() - o.getMonth();
            if (x!=0)
                return x;
            else
            {
                x = this.getDay() - o.getDay();
                if (x != 0)
                    return x;
                else
                {
                    x = this.getHours() - o.getHours();
                    if (x !=0 )
                        return x;
                    else
                    {
                        x = this.getMinutes() - o.getMinutes();
                        return x;
                    }
                }
            }
        }
            
    }
    
}
